using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Visio = Microsoft.Office.Interop.Visio;
using WorkflowRelevantData = ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per FormAttributes.
	/// </summary>
	public class FormAttributes : System.Windows.Forms.Form
	{
		private Visio.Shape m_Shape;

		private WorkflowRelevantData.XMLAttributes xmlAttributes;
		private WorkflowRelevantData.XMLAttributes xmlNotRemovableAttributes;

		private System.Windows.Forms.ListView listAttributes;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnName;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem addToolStripMenuItem;
        private ToolStripMenuItem modifyToolStripMenuItem;
        private ToolStripMenuItem removeToolStripMenuItem;
        private IContainer components;

		public FormAttributes()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
			xmlAttributes = new WorkflowRelevantData.XMLAttributes();

			this.BuildGridHeader();
		}

		public FormAttributes(string sAttributes) : this()
		{
			if ( sAttributes != "" )
			{
				xmlAttributes.Parse ( sAttributes);
				this.FillForm ();
			}
		}

		public FormAttributes(ref WorkflowRelevantData.XMLAttributes vAttributes) : this()
		{
			if ( vAttributes != null )
			{
				xmlAttributes = vAttributes;

				this.FillForm ();
			}
		}

		public FormAttributes(ref WorkflowRelevantData.XMLAttributes vAttributes, WorkflowRelevantData.XMLAttributes vNotRemovableAttrs) : this()
		{
			xmlNotRemovableAttributes = vNotRemovableAttrs;

			if ( vAttributes != null )
			{
				xmlAttributes = vAttributes;

				this.FillForm ();
			}

			
		}

		public FormAttributes(Visio.Shape oSelectedShape): this(oSelectedShape.Data1)
		{
			this.Text="Attributes:" + oSelectedShape.Text;
			m_Shape=oSelectedShape;
		}

		public WorkflowRelevantData.XMLAttributes XMLAttributes
		{
			get
			{
				return xmlAttributes;
			}
		}

		private void BuildGridHeader()
		{
			listAttributes.Clear();

			listAttributes.Columns.Add("Name",100, HorizontalAlignment.Left);
			listAttributes.Columns.Add("Type",100, HorizontalAlignment.Left);
			listAttributes.Columns.Add("Direction",50, HorizontalAlignment.Left);
			listAttributes.Columns.Add ("Value", listAttributes.Width - 250, HorizontalAlignment.Left);
		}

		private void FillForm()
		{
			this.BuildGridHeader();

			for ( int i=0; i<xmlAttributes.Count; i++)
			{
				WorkflowRelevantData.XMLAttribute xmlAttribute = (WorkflowRelevantData.XMLAttribute)xmlAttributes[i];

				ListViewItem item = new ListViewItem( new string[] { xmlAttribute.Name, xmlAttribute.Type, xmlAttribute.Direction, xmlAttribute.Value});
				
				if ( xmlNotRemovableAttributes != null )
				{
					try
					{
						WorkflowRelevantData.XMLAttribute attribute = xmlNotRemovableAttributes.Get ( xmlAttribute.Name);
						
						item.BackColor = Color.Red; 

					}
					catch ( ScenarioFramework.WorkflowEngine.DataModel.Exception.AttributeNotFound )
					{
						
					}
				}

				listAttributes.Items.Add (item);
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.listAttributes = new System.Windows.Forms.ListView();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnName = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.addToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.modifyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.panel2.SuspendLayout();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // listAttributes
            // 
            this.listAttributes.ContextMenuStrip = this.contextMenuStrip1;
            this.listAttributes.Location = new System.Drawing.Point(7, 7);
            this.listAttributes.Name = "listAttributes";
            this.listAttributes.Size = new System.Drawing.Size(826, 464);
            this.listAttributes.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.listAttributes.TabIndex = 0;
            this.listAttributes.UseCompatibleStateImageBehavior = false;
            this.listAttributes.View = System.Windows.Forms.View.Details;
            this.listAttributes.DoubleClick += new System.EventHandler(this.listAttributes_DoubleClick);
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnName);
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 317);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(835, 42);
            this.panel2.TabIndex = 15;
            // 
            // btnName
            // 
            this.btnName.Location = new System.Drawing.Point(700, 3);
            this.btnName.Name = "btnName";
            this.btnName.Size = new System.Drawing.Size(60, 30);
            this.btnName.TabIndex = 12;
            this.btnName.Text = "XML";
            this.btnName.Click += new System.EventHandler(this.btnName_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(7, 3);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 30);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(766, 3);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(60, 30);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.addToolStripMenuItem,
            this.modifyToolStripMenuItem,
            this.removeToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(125, 70);
            // 
            // addToolStripMenuItem
            // 
            this.addToolStripMenuItem.Name = "addToolStripMenuItem";
            this.addToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.addToolStripMenuItem.Text = "Add";
            this.addToolStripMenuItem.Click += new System.EventHandler(this.addToolStripMenuItem_Click);
            // 
            // modifyToolStripMenuItem
            // 
            this.modifyToolStripMenuItem.Name = "modifyToolStripMenuItem";
            this.modifyToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.modifyToolStripMenuItem.Text = "Modify";
            this.modifyToolStripMenuItem.Click += new System.EventHandler(this.modifyToolStripMenuItem_Click);
            // 
            // removeToolStripMenuItem
            // 
            this.removeToolStripMenuItem.Name = "removeToolStripMenuItem";
            this.removeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.removeToolStripMenuItem.Text = "Remove";
            this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click);
            // 
            // FormAttributes
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(835, 359);
            this.ControlBox = false;
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.listAttributes);
            this.Name = "FormAttributes";
            this.Text = "Process: Attributes";
            this.panel2.ResumeLayout(false);
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			xmlAttributes.Clear();

			for ( int i=0; i<listAttributes.Items.Count; i++)
			{
				WorkflowRelevantData.XMLAttribute attribute = this.GetAttribute (listAttributes.Items[i]);

				xmlAttributes.Add ( attribute);
			}

			if ( m_Shape != null )
			{
				m_Shape.Data1=xmlAttributes.GetXML();
			}

			this.Close();

		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			
		}

		private void btnModify_Click(object sender, System.EventArgs e)
		{
			
		}

		private void btnRemove_Click(object sender, System.EventArgs e)
		{
			
		}

		private WorkflowRelevantData.XMLAttribute GetAttribute(ListViewItem item)
		{
			WorkflowRelevantData.XMLAttribute xmlAttribute = new WorkflowRelevantData.XMLAttribute();

			xmlAttribute.Name = item.SubItems[0].Text;
			xmlAttribute.Type  = item.SubItems[1].Text;
			xmlAttribute.Direction  = item.SubItems[2].Text;
			xmlAttribute.Value = item.SubItems[3].Text;

			return xmlAttribute;
		}

		private void listAttributes_DoubleClick(object sender, System.EventArgs e)
		{
			this.btnModify_Click(listAttributes, null);
		}

		private void btnName_Click(object sender, System.EventArgs e)
		{
			FormXML frmXML = new FormXML();
			if ( xmlAttributes != null )
			{
				frmXML.XML = xmlAttributes.GetXML();
			}
			frmXML.ShowDialog(this);

			if ( frmXML.OK )
			{
				xmlAttributes.Clear();
				xmlAttributes.Parse ( frmXML.XML);
				if ( m_Shape != null )
				{	
					m_Shape.Data1=frmXML.XML;
				}
			}
			this.Close();
		}

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAttribute frmAttribute = new FormAttribute();
            frmAttribute.ShowDialog(this);

            if (frmAttribute.xmlAttribute != null)
            {
                ListViewItem item = new ListViewItem(new string[] { frmAttribute.xmlAttribute.Name, frmAttribute.xmlAttribute.Type, frmAttribute.xmlAttribute.Direction, frmAttribute.xmlAttribute.Value });
                listAttributes.Items.Add(item);
                listAttributes.Refresh();
            }
        }

        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listAttributes.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select one attribute.");
                return;
            }
            else if (listAttributes.SelectedItems.Count > 1)
            {
                MessageBox.Show("Select only one attribute.");
                return;
            }

            WorkflowRelevantData.XMLAttribute xmlAttribute = this.GetAttribute(listAttributes.SelectedItems[0]);

            FormAttribute frmAttribute = new FormAttribute(xmlAttribute);
            frmAttribute.ShowDialog();

            if (frmAttribute.xmlAttribute != null)
            {
                listAttributes.SelectedItems[0].SubItems[0].Text = frmAttribute.xmlAttribute.Name;
                listAttributes.SelectedItems[0].SubItems[1].Text = frmAttribute.xmlAttribute.Type;
                listAttributes.SelectedItems[0].SubItems[2].Text = frmAttribute.xmlAttribute.Direction;
                listAttributes.SelectedItems[0].SubItems[3].Text = frmAttribute.xmlAttribute.Value;
            }
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listAttributes.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select one attribute.");
                return;
            }

            string sMessage = string.Empty;

            for (int i = 0; i < listAttributes.SelectedItems.Count; i++)
            {
                if (xmlNotRemovableAttributes == null)
                {
                    listAttributes.Items.RemoveAt(listAttributes.SelectedItems[i].Index);
                }
                else
                {
                    try
                    {
                        WorkflowRelevantData.XMLAttribute attribute = xmlNotRemovableAttributes.Get(listAttributes.SelectedItems[i].SubItems[0].Text);
                        sMessage += attribute.Name + " ";
                    }
                    catch (ScenarioFramework.WorkflowEngine.DataModel.Exception.AttributeNotFound)
                    {
                        listAttributes.Items.RemoveAt(listAttributes.SelectedItems[i].Index);
                    }
                }
            }

            if (sMessage != string.Empty)
            {
                MessageBox.Show("Impossible to delete mandatory attributes[" + sMessage + "]");
            }
        }
	}
}
